﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using eBay;
using System.Collections;
using System.Xml;

namespace Common {
    public class Settings {
        public Dictionary<string, string> InnerSettings;

        public Settings() {
            InnerSettings = new Dictionary<string, string>();
            string[] setArr = File.ReadAllLines(Funcs.GetAppPath() + "ebay.config");
            foreach (string line in setArr) {
                if (line.Trim() == "" || line.Trim().StartsWith("//")) continue;
                string[] keyVal = line.Split('=');
                if (keyVal[0].Trim() == "" || keyVal[1].Trim() == "") continue;
                InnerSettings.Add(keyVal[0].Trim(), keyVal[1].Trim());
            }

            if (InnerSettings["DevID"] == ""
                || InnerSettings["AppIDSandbox"] == ""
                || InnerSettings["AppIDProduct"] == ""
                || InnerSettings["CertID"] == ""
                || InnerSettings["UserToken"] == ""
                || InnerSettings["ServerUrl"] == ""
                || InnerSettings["SiteID"] == "")
                throw new Exception();
        }

        public string DevID { get { return InnerSettings["DevID"]; } }
        public string AppIDSandbox { get { return InnerSettings["AppIDSandbox"]; } }
        public string AppIDProduct { get { return InnerSettings["AppIDProduct"]; } }
        public string CertID { get { return InnerSettings["CertID"]; } }
        public string UserToken { get { return InnerSettings["UserToken"]; } }
        public string ServerUrl { get { return InnerSettings["ServerUrl"]; } }
        public string SiteID { get { return InnerSettings["SiteID"]; } }

        public string ShoppingAPIUrl { get { return InnerSettings["ShoppingAPIUrl"]; } }
        public string FindingAPIUrl { get { return InnerSettings["FindingAPIUrl"]; } }
    }

    public class Funcs {
        public static string GetAppPath() {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

            path = path.Substring(0, path.LastIndexOf(@"\"));

            string curFolder = path.Substring(path.LastIndexOf(@"\"));
            if (curFolder == "\\Debug" || curFolder == "\\Release") {
                path = path + "\\..\\..\\";
            }
            else {
                path = path + "\\";
            }

            return path;
        }
    }

    public static class Global {
        public static Settings Configuration = new Settings();
        public static Hashtable Cache = new Hashtable();
        public static Dictionary<eBaySites, string> eBaySiteNames = new Dictionary<eBaySites, string>();

        public static void FilleBaySiteNames() {
            eBaySiteNames[eBaySites.EBAY_AT] = "eBay Austria";
            eBaySiteNames[eBaySites.EBAY_AU] = "eBay Australia";
            eBaySiteNames[eBaySites.EBAY_CH] = "eBay Switzerland";
            eBaySiteNames[eBaySites.EBAY_DE] = "eBay Germany";
            eBaySiteNames[eBaySites.EBAY_ENCA] = "eBay Canada (English)";
            eBaySiteNames[eBaySites.EBAY_ES] = "eBay Spain";
            eBaySiteNames[eBaySites.EBAY_FR] = "eBay France";
            eBaySiteNames[eBaySites.EBAY_FRBE] = "eBay Belgium (French)";
            eBaySiteNames[eBaySites.EBAY_FRCA] = "eBay Canada (French)";
            eBaySiteNames[eBaySites.EBAY_GB] = "eBay UK";
            eBaySiteNames[eBaySites.EBAY_HK] = "eBay Hong Kong";
            eBaySiteNames[eBaySites.EBAY_IE] = "eBay Ireland";
            eBaySiteNames[eBaySites.EBAY_IN] = "eBay India";
            eBaySiteNames[eBaySites.EBAY_IT] = "eBay Italy";
            eBaySiteNames[eBaySites.EBAY_MOTOR] = "eBay Motors";
            eBaySiteNames[eBaySites.EBAY_MY] = "eBay Malaysia";
            eBaySiteNames[eBaySites.EBAY_NL] = "eBay Netherlands";
            eBaySiteNames[eBaySites.EBAY_NLBE] = "eBay Belgium (Dutch)";
            eBaySiteNames[eBaySites.EBAY_PH] = "eBay Philippines";
            eBaySiteNames[eBaySites.EBAY_PL] = "eBay Poland";
            eBaySiteNames[eBaySites.EBAY_SE] = "eBay Sweden";
            eBaySiteNames[eBaySites.EBAY_SG] = "eBay Singapore";
            eBaySiteNames[eBaySites.EBAY_US] = "eBay United States";

        }

        private static XmlDocument InnerCategoriesTree = null;
        public static XmlDocument CategoriesTree {
            get {
                if (InnerCategoriesTree != null)
                    return InnerCategoriesTree;
                else {
                    InnerCategoriesTree = Categories.GetCategories();
                    return InnerCategoriesTree;
                }
            }
        }
    }
}
